package top.llove.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.llove.service.AdminService;
import top.llove.utils.res.ApiResult;

import javax.servlet.http.HttpServletRequest;

/**
 * 管理员Controller
 *
 * @author: 幸会
 * @date: 2021/6/8 10:59
 * @email: 1846724873@qq.com
 * @description: 管理员Controller
 * version: 1.0
 */
@RestController
@SessionAttributes("public")
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService admin; // 管理员Service

    /**
     * 注册管理员
     * @param user 用户名
     * @param pass 密码
     * @param head 头像
     * @param email 邮箱
     * @return
     */
    @PostMapping("/xh/r")
    public ApiResult save(@RequestParam(value = "u", required = true) String user,
                          @RequestParam(value = "p", required = true) String pass,
                          @RequestParam(value = "h", required = true) String head,
                          @RequestParam(value = "e", required = true) String email) {
        return admin.save(user, pass, head, email);
    }

    /**
     * 登录
     * @param user 用户名
     * @param pass 密码
     * @param verify 验证码
     * @return
     */
    @PostMapping("/xh/l")
    public ApiResult login(@RequestParam(value = "u", required = true) String user,
                           @RequestParam(value = "p", required = true) String pass,
                           @RequestParam(value = "v", required = true) String verify,
                           HttpServletRequest req) {
        return admin.login(user, pass, verify, req);
    }


    /**
     * 修改管理员信息
     * @param id 要修改的管理员id
     * @param pass 新管理员密码
     * @param head 新管理员头像
     * @param email 新管理员邮箱
     * @return
     */
    @PostMapping("/xh/u")
    public ApiResult update(@RequestParam(value = "i", required = true) String id,
                            @RequestParam(value = "p", required = false) String pass,
                            @RequestParam(value = "h", required = false) String head,
                            @RequestParam(value = "e", required = false) String email) {
        return admin.update(id, pass, head, email);
    }

    /**
     * 管理员退出登录
     * @return
     */
    @GetMapping("/xh/e")
    public ApiResult exit(HttpServletRequest req) {
        return admin.exit(req);
    }
}
